//
//  RecommendViewController.m
//  jinling
//
//  Created by mac on 2018/9/20.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "RecommendViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>


@interface RecommendViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
@property (strong, nonatomic) WKWebView *myWebView;

@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self wr_setNavBarBackgroundAlpha:0];
  
    _cengsu=1;
    
    
    WKWebViewConfiguration* configuration = [[NSClassFromString(@"WKWebViewConfiguration") alloc] init];
    
    configuration.processPool = [WKProcessPool sharedProcessPool];
    [configuration.userContentController addScriptMessageHandler:self name:@"push"];
    
    
    self.myWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT, _mainW, _mainH-NAV_HEIGHT) configuration:configuration];
    
    self.myWebView.navigationDelegate = self;
    self.myWebView.UIDelegate=self;
    [self.view addSubview:self.myWebView];
    
    
    NSURL *url;
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    
     if (userDic!=nil) {
        NSString *oppenid=[userDic objectForKey:@"oppenid"];
        NSString *user_account=[userDic objectForKey:@"user_account"];
        url =[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@&oppen_id=%@&user_account=%@",_addressUrl,@"page/recommend-goods.html?cat_id=-1",oppenid,user_account]];
        
    }
    else{
         url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",_addressUrl,@"page/recommend-goods.html?cat_id=-1"]];
    }
    
   
   NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];

    
    [self.myWebView loadRequest:mutableRequest];
    
    
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, CGRectGetWidth(self.view.frame), 2) ];
    self.progressView.progressTintColor =_backgroundColor;
    [self.view addSubview:self.progressView];
    

    
    [self.myWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [self.myWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    
    
    
}



-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{

     [self.progressView setProgress:0.0f animated:YES];
//    [MBProgressHUD hideHUDForView:self.view animated:YES];

}


#pragma mark KVO的监听代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
   

    if ([keyPath isEqualToString:@"title"]){//网页title
        if (object == self.myWebView){
            self.navigationItem.title = self.myWebView.title;
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
        
    }else if ([keyPath isEqual:@"estimatedProgress"] && object == self.myWebView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.myWebView.estimatedProgress animated:YES];
        if (self.myWebView.estimatedProgress  >= 1.0f) {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:YES];
            }];
        }
    }
    else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    
    if ([message.name isEqualToString:@"push"]) {
        

        
        if (_cengsu==1) {
            TWOViewController *two=[[TWOViewController alloc] init];
            two.url=message.body;
                two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==2) {
            ThreeViewController *two=[[ThreeViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==3) {
            FourViewController *two=[[FourViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==4) {
            FiveViewController *two=[[FiveViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==5) {
            SixViewController *two=[[SixViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==6) {
            SevenViewController *two=[[SevenViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==7) {
            EightViewController *two=[[EightViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==8) {
            NineViewController *two=[[NineViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;
            [gongju pushNextViewController:self andRootController:two];
        }
        if (_cengsu==9) {
            TenViewController *two=[[TenViewController alloc] init];
            two.url=message.body;
            two.cengsu=_cengsu+1;
            two.isHave=YES;   
            [gongju pushNextViewController:self andRootController:two];
        }
    }
}


- (void)dealloc {
    [self.myWebView removeObserver:self forKeyPath:@"title"];
    [self.myWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
